package cn.edu.nwafu.core.controller.admin;

import cn.edu.nwafu.common.exception.Assert;
import cn.edu.nwafu.common.result.R;
import cn.edu.nwafu.common.result.ResponseEnum;
import cn.edu.nwafu.core.pojo.entity.UserInfo;
import cn.edu.nwafu.core.pojo.query.UserInfoQuery;
import cn.edu.nwafu.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/admin/core/userInfo")
@Api(tags = "用户管理")
public class AdminUserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("获取用户分页列表")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(value = "查询对象", required = false) UserInfoQuery userInfoQuery
    ) {
        Page<UserInfo> pageParam = new Page<>(page, limit);

        IPage<UserInfo> pageModel = userInfoService.listPage(pageParam, userInfoQuery);

        return R.ok().data("pageModel", pageModel);
    }

    @ApiOperation("添加用户信息")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "新增用户对象", required = true)
            @RequestBody UserInfo userInfo
    ) {
        Assert.notEmpty(userInfo.getName(), ResponseEnum.ADD_NAME_EMPTY);
        Assert.notNUll(userInfo.getPassengerType(), ResponseEnum.ADD_PASSENGER_TYPE_EMPTY);
        Assert.notEmpty(userInfo.getIdCard(), ResponseEnum.ADD_IDCARD_EMPTY);
        Assert.notEmpty(userInfo.getMail(), ResponseEnum.ADD_MAIL_EMPTY);
        Assert.notEmpty(userInfo.getMobile(), ResponseEnum.ADD_MOBILE_EMPTY);
        Assert.notEmpty(userInfo.getPassword(), ResponseEnum.ADD_PASSWORD_EMPTY);
        userInfo.setHeadImg(UserInfo.USER_AVATOR);

        userInfoService.saveUser(userInfo);

        return R.ok().message("新增用户成功");
    }

    @ApiOperation("删除用户信息")
    @DeleteMapping("/delete/{id}")
    public R removeById(
            @ApiParam(value = "所删用户id", required = true)
            @PathVariable Long id
    ) {

        boolean result = userInfoService.removeById(id);

        return result ? R.ok().message("该用户删除成功") : R.error().message("该用户删除失败");

    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value = "所查询用户的id", required = true)
            @PathVariable Long id
    ) {

        UserInfo userInfo = userInfoService.getById(id);

        return userInfo != null ? R.ok().data("user", userInfo) : R.error().message("获取用户信息失败");
    }

    @ApiOperation("根据id修改用户信息")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(value = "所修改用户的信息", required = true)
            @RequestBody UserInfo userInfo
    ) {

        boolean result = userInfoService.updateById(userInfo);

        return result ? R.ok().message("用户信息更新成功") : R.error().message("用户信息更新失败");
    }
}
